using System;
using System.Collections.Generic;
using Dune.Business.Domain.Users;
using Edge.Common.ComponentModel;
using Edge.Common.Services;
using Edge.Business.Entities;


namespace Dune.Business.Domain.Forum
{
    #region Theme

    /// <summary>
    /// Theme object for NHibernate mapped table 'themes'.
    /// </summary>
    ///        
    [Serializable]
 //   [Designer("Dune.Common.Web.EntityDesigner.CategoryDesignerAttribute")]
    public class Theme : EntityWithStat<int>, IEquatable<Theme>
    {
        #region Member Variables

        protected string _description;
        protected string _name;
        protected string imageSrc;
        protected Theme _parentTheme;
        protected ICollection<Theme> children;
        protected ICollection<Post> posts;
        protected bool top;
        protected RoleType? visibleFor;

        #endregion


        #region Constructors

        public Theme() { }

        public Theme(string Name, string Description, Theme ParentTheme)
        {
            this._parentTheme = ParentTheme;
            this._description = Description;
            this._name = Name;
            this.CreateDate = DateTime.Now;
        }


        public Theme(string _name, string _description, Theme _parentTheme, UserEntity<int> createdBy, bool top)
        {
            this._description = _description;
            this._name = _name;
            this._parentTheme = _parentTheme;
            this.CreatedBy = createdBy;
            this.top = top;
            this.CreateDate = DateTime.Now;
        }

        #endregion


        #region Public Properties

        public virtual string Description
        {
            get { return _description; }
            set
            {
                if (value != null && value.Length > 250)
                    throw new ArgumentOutOfRangeException("Invalid value for Description", value, value.ToString());
                _description = value;
            }
        }


        public virtual string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public virtual string ImageSrc
        {
            get { return imageSrc; }
            set { imageSrc = value; }
        }

        public virtual Theme ParentTheme
        {
            get { return _parentTheme; }
            set { _parentTheme = value; }
        }

        public virtual ICollection<Theme> Children
        {
            get
            {
                return children;
            }
            set
            {
                children = value;
            }
        }


        public virtual ICollection<Post> Posts
        {
            get { return posts; }
            set { posts = value; }
        }


        public virtual bool Top
        {
            get { return top; }
            set { top = value; }
        }

        public virtual string FullName
        {
            get
            {
                string s = Name;
                Theme c = ParentTheme;
                while (c != null)
                {
                    s = c.Name + " > " + s;
                    c = c.ParentTheme;
                }
                return s;
            }
        }

        public virtual Theme LastUpdatedTheme
        {
            get
            {
                return Locator.GetService<IThemeManager>().GetLastUpdatedTheme(this);
            }
        }

        #endregion

        public bool Equals(Theme theme)
        {
            if (theme == null) return false;
            if (!Equals(_name, theme._name)) return false;
            if (!Equals(_description, theme._description)) return false;
            if (!Equals(_parentTheme, theme._parentTheme)) return false;
            if (!Equals(children, theme.children)) return false;
            return true;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(this, obj)) return true;
            return Equals(obj as Theme);
        }

        public override int GetHashCode()
        {
            int result = _description != null ? _description.GetHashCode() : 0;
            result = 29 * result + (_parentTheme != null ? _parentTheme.GetHashCode() : 0);
            result = 29 * result + (children != null ? children.GetHashCode() : 0);
            return result;
        }

        public virtual NavigationItem ConvertToNavigation()
        {
            return ConvertToNavigation(false);
        }

        public virtual NavigationItem ConvertToNavigation(bool current)
        {
            NavigationItem ret = null;
            ret = new NavigationItem(this.Name, this.Id.ToString());
            NavigationItem[] childrenNavigation = new NavigationItem[children.Count];
            int i = 0;
            foreach (Theme theme in children)
            {
                childrenNavigation[i] = new NavigationItem(theme.Name, theme.Id.ToString());
                i++;
            }
            ret.Childrens = childrenNavigation;
            if (current)
                ret.Current = true;
            return ret;
        }

        public virtual RoleType? VisibleFor
        {
            get { return visibleFor; }
            set { visibleFor = value; }
        }
    }



    #endregion
}





